﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using Business;

namespace QuanLyNhaSach
{
    public partial class CapNhatTacGia : Form
    {
        public CapNhatTacGia()
        {
            InitializeComponent();
        }
        private void SetUpDataGridViewTacGia()
        {
            dgvTacGia.BackgroundColor = Color.White;
            dgvTacGia.ColumnCount = 2;
            dgvTacGia.Columns[0].Name = "Mã Tác Giả";
            dgvTacGia.Columns[1].Name = "Tên Tác Giả";

            dgvTacGia.Columns[0].ReadOnly = true;

            dgvTacGia.Columns[0].Width = 80;
            dgvTacGia.Columns[1].Width = 240;

            dgvTacGia.AllowUserToAddRows = false;
            dgvTacGia.AllowUserToDeleteRows = false;
        }
        private void LoadDuLieuDataGridViewTacGia()
        {
            List<TacGiaDTO> list = new List<TacGiaDTO>();
            list = TacGiaService.GetAllTacGia();
            foreach (TacGiaDTO tacgia in list)
            {
                DataGridViewRow row = new DataGridViewRow();
                row.CreateCells(dgvTacGia);
                row.Cells[0].Value = tacgia.MaTacGia.ToString();
                row.Cells[1].Value = tacgia.TenTacGia.ToString();
                dgvTacGia.Rows.Add(row);
            }
        }
        private void CapNhatTacGia_Load(object sender, EventArgs e)
        {
            SetUpDataGridViewTacGia();
            LoadDuLieuDataGridViewTacGia();
        }

        private void dgvTacGia_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            TacGiaDTO tacgiaDTO = new TacGiaDTO();
            tacgiaDTO.MaTacGia=int.Parse(dgvTacGia.Rows[dgvTacGia.SelectedCells[0].RowIndex].Cells[0].Value.ToString());
            tacgiaDTO.TenTacGia=dgvTacGia.Rows[dgvTacGia.SelectedCells[0].RowIndex].Cells[1].Value.ToString();
            TacGiaService tacgiaservice=new TacGiaService();
            tacgiaservice.Update(tacgiaDTO);

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
